<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>AI知行学堂</h1>
        <p>AI+X 智学习课，培养实战型复合人才</p>
      </div>

      <div class="login-tabs">
        <div class="tab active">学生登录</div>
        <div class="tab">教师登录</div>
      </div>

      <a-form :model="formState" name="basic" autocomplete="off" class="login-form" @finish="onFinish">
        <a-form-item name="userName" :rules="[{ required: true, message: '请输入邮箱' }]">
          <a-input v-model:value="formState.userName" placeholder="请输入邮箱" size="large">
            <template #prefix>
              <MailOutlined />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item name="password" :rules="[{ required: true, message: '请输入密码' }]">
          <a-input-password v-model:value="formState.password" placeholder="请输入密码" size="large">
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>

        <!-- <div class="form-options">
          <a-checkbox v-model:checked="formState.remember">记住我</a-checkbox>
          <a class="forgot-password">忘记密码？</a>
        </div> -->

        <a-form-item>
          <a-button type="primary" html-type="submit" size="large" block :loading="loading">
            登录
          </a-button>
        </a-form-item>

        <div class="register-link">
          还没有账号？<a>立即注册</a>
        </div>

        <div class="test-account">
          使用学生测试账户
        </div>
      </a-form>

      <div class="version-info">
        系统演示版本
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { MailOutlined, LockOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import { useRouter } from 'vue-router';
import { useAppStore } from '@/store';
import { LoginInput } from '@/api/services/types';
import { UserApi } from '@/api/services';

const router = useRouter();
const appStore = useAppStore();
const formState = reactive<LoginInput>({
  userName: 'admin@abp.io',
  password: '123456',
});

const loading = ref(false);

const onFinish = (values: any) => {
  loading.value = true;
  console.log('Success:', values);
  const input = Object.assign({}, formState);
  UserApi.login(input).then(res => {
    const { code, data,msg } = res;
    if (code == 0) {
      appStore.setToken(data);
      getApplication();
    }else{
      message.error(msg)
    }
  }).catch(() => {
    loading.value = false;
  })

};

const getApplication = function(){
  UserApi.getContextUser().then(res=>{
    const { code, data } = res;
    if(code==0){
      appStore.setContextUser(data);
      message.success('登录成功');
      router.push({path:'/common/settings'})
    }
  }).finally(()=>{
    loading.value = false;
  })
}
</script>

<style scoped lang="less">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 420px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 40px;
  text-align: center;
}

.login-header {
  margin-bottom: 30px;

  h1 {
    font-size: 24px;
    color: #333;
    margin-bottom: 8px;
    font-weight: 600;
  }

  p {
    font-size: 14px;
    color: #666;
    margin: 0;
  }
}

.login-tabs {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 15px;

  .tab {
    padding: 0 20px;
    cursor: pointer;
    color: #666;
    font-size: 16px;
    position: relative;

    &.active {
      color: #1890ff;
      font-weight: 500;

      &::after {
        content: '';
        position: absolute;
        bottom: -16px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 2px;
        background-color: #1890ff;
      }
    }
  }
}

.login-form {
  text-align: left;

  :deep(.ant-input-affix-wrapper) {
    padding: 8px 11px;
  }

  :deep(.ant-input-prefix) {
    margin-right: 8px;
    color: rgba(0, 0, 0, 0.25);
  }
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;

  .forgot-password {
    font-size: 14px;
  }
}

.register-link {
  text-align: center;
  margin: 16px 0;
  font-size: 14px;
  color: #666;

  a {
    color: #1890ff;
  }
}

.test-account {
  text-align: center;
  margin: 24px 0;
  color: #1890ff;
  font-size: 14px;
  cursor: pointer;
}

.version-info {
  text-align: center;
  color: #999;
  font-size: 12px;
  margin-top: 20px;
}

@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
  }

  .login-header h1 {
    font-size: 20px;
  }

  .login-tabs .tab {
    padding: 0 15px;
    font-size: 14px;
  }
}
</style>